package com.example.campusforumapi.service.impl;

import com.example.campusforumapi.dao.DepartmentMapper;
import com.example.campusforumapi.entity.Department;
import com.example.campusforumapi.entity.User;
import com.example.campusforumapi.service.DepartmentService;
import com.example.campusforumapi.util.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public PageResult getDepartmentPage(Integer page, Integer PageSize){
        //1.设置分页参数
        PageHelper.startPage(page,PageSize);
//        2.执行查询
        List<Department> departmentList = departmentMapper.list();
        Page<Department> p = (Page<Department>) departmentList;
//        3.封装PageResult对象
        PageResult pageResult = new PageResult(p.getResult(),p.getTotal(),p.getPageSize(),p.getPageNum());
        return pageResult;
    }

    @Override
    public int delDepartment(List<Integer> ids){ return departmentMapper.delDepartment(ids); }

    @Override
    public int findsamedepartmentName(String departmentName) {
        return departmentMapper.findsamedepartmentName(departmentName);
    }

    @Override
    public int findsameDepartmentId(Integer departmentId) {
        return departmentMapper.findsameDepartmentId(departmentId);
    }

    @Override
    public int insertDepartment(Department department) {
        department.setCreateTime(LocalDateTime.now());
        return departmentMapper.insertDepartment(department);
    }

    @Override
    public int updDepartment(Department department) {
        department.setUpdateTime(LocalDateTime.now());//更新修改时间
        return departmentMapper.upDepartment(department);
    }

}
